﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace GUI
{
    public partial class ucKhuVuc : UserControl
    {
        public ucKhuVuc()
        {
            InitializeComponent();
        }
        KhuVucDTO khuvuc =  new KhuVucDTO();
        List<int> r = new List<int>();
        int j = 0;

        private void ucKhuVuc_Load(object sender, EventArgs e)
        {
            pnThem.Visible = false;
            pnDanhSach.Dock = DockStyle.Fill;
            DataTable dt = KhuVucBus.DanhSachKhuVuc();
            dgvKhuVuc.DataSource = dt;
        }

        private void tsDanhSachKhuVuc_Click(object sender, EventArgs e)
        {
            pnDanhSach.Visible = true;
            pnDanhSach.Dock = DockStyle.Fill;
            pnThem.Visible = false;
        }

        private void tsThemKhuVuc_Click(object sender, EventArgs e)
        {
            pnThem.Dock = DockStyle.Fill;
            pnThem.Visible = true;
            pnDanhSach.Visible = false;
        }

        private void btnThoat2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void dgvKhuVuc_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            int r = dgvKhuVuc.CurrentCell.RowIndex;
            khuvuc.TenKhuVuc = dgvKhuVuc.Rows[r].Cells[1].Value.ToString();
            khuvuc.SoGioNang = double.Parse(dgvKhuVuc.Rows[r].Cells[2].Value.ToString());
        }

        private void dgvKhuVuc_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvKhuVuc.Rows[e.RowIndex].Cells[1].Value.ToString() == khuvuc.TenKhuVuc)
                r.Add(e.RowIndex);
            else if (KhuVucBus.KiemTraKhuVuc(dgvKhuVuc.Rows[e.RowIndex].Cells[1].Value.ToString()) == 1)
                r.Add(e.RowIndex);
            else
            {
                MessageBox.Show(dgvKhuVuc.Rows[e.RowIndex].Cells[1].Value.ToString() + " đã có");
                dgvKhuVuc.Rows[e.RowIndex].Cells[1].Value = khuvuc.TenKhuVuc;
            }
            switch (e.ColumnIndex)
            {
                case 1:
                    if (dgvKhuVuc.CurrentCell.Value == DBNull.Value)
                        dgvKhuVuc.CurrentCell.Value = khuvuc.TenKhuVuc;
                    break;
                case 2:
                    if (dgvKhuVuc.CurrentCell.Value == DBNull.Value)
                        dgvKhuVuc.CurrentCell.Value = khuvuc.SoGioNang;
                    break;
            }
        }

        private void dgvKhuVuc_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = e.Control as TextBox;
            txt.Name = "";
            if (this.dgvKhuVuc.CurrentCellAddress.X == dgvKhuVuc.Columns[2].DisplayIndex)
            {
                txt.Name = "Col";
                if (txt != null)
                {
                    txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
                }
            } 
        }

        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Kiem tra
            TextBox txt = sender as TextBox;
            if (txt.Name == "Col")
            {
                try
                {
                    //Không đc nhập chữ và cho phép sử dụng dấu '.', backspace
                    if (!Char.IsDigit(e.KeyChar) && (!Char.IsControl(e.KeyChar)) && e.KeyChar != '.')
                        e.Handled = true;
                }
                catch { };
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            KhuVucDTO kv = new KhuVucDTO();
            for (int i = 0; i < r.Count; i++)
            {
                kv.MaKhuVuc = Int32.Parse(dgvKhuVuc.Rows[r[i]].Cells[0].Value.ToString());
                kv.TenKhuVuc = dgvKhuVuc.Rows[r[i]].Cells[1].Value.ToString();
                kv.SoGioNang = double.Parse(dgvKhuVuc.Rows[r[i]].Cells[2].Value.ToString());

                KhuVucBus.CapNhatKhuVuc(kv);
            }
            DataTable dt = KhuVucBus.DanhSachKhuVuc();
            dgvKhuVuc.DataSource = dt;
        }

        private void btnLamMoi2_Click(object sender, EventArgs e)
        {
            DataTable dt = KhuVucBus.DanhSachKhuVuc();
            dgvKhuVuc.DataSource = dt;
        }

        private void dgvChiTietKhuVuc_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            switch (e.ColumnIndex)
            {
                case 0:
                    //Ktra nhập liệu
                    if (dgvChiTietKhuVuc.CurrentCell.Value != null) 
                    {
                        if (HopLe(dgvChiTietKhuVuc, dgvChiTietKhuVuc.Rows[e.RowIndex].Cells[0].Value.ToString(), iRow) == 1)
                        {
                            if (KhuVucBus.KiemTraKhuVuc(dgvChiTietKhuVuc.Rows[e.RowIndex].Cells[0].Value.ToString()) == 0)
                            {
                                MessageBox.Show(dgvChiTietKhuVuc.Rows[e.RowIndex].Cells[0].Value.ToString() + " đã có");
                                dgvChiTietKhuVuc.Rows[e.RowIndex].Cells[0].Value = "";
                                //dgvChiTietThietBiThongDung.CurrentRow.Selected = true;
                            }
                            else
                            {
                                if (iRow > -1)
                                {
                                    j = iRow + 1;
                                    iRow = -1;
                                }
                            }
                        }
                        else
                        {
                            MessageBox.Show("Khu vực đã có ở trên");
                            dgvChiTietKhuVuc.Rows[e.RowIndex].Cells[0].Value = null;
                        }
                    }
                    break;
            }
        }

        private void dgvChiTietKhuVuc_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = e.Control as TextBox;
            txt.Name = "";
            if (this.dgvChiTietKhuVuc.CurrentCellAddress.X == dgvChiTietKhuVuc.Columns[1].DisplayIndex)
            {
                txt.Name = "Col";
                if (txt != null)
                {
                    txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
                }
            } 
        }

        private void btnThoat_TraCuu_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLamMoi_TraCuu_Click(object sender, EventArgs e)
        {
            txtTenKhuVuc.Text = "";
            txtSoGioNang.Value = 1;
            txtTenKhuVuc.Focus();
            dgvChiTietKhuVuc.Rows.Clear();
            j = 0;
            iRow = -1;
        }

        private int HopLe(DataGridView data, string str,int count)
        {
            int i = 0;
            while (i < count)
            {
                if (data.Rows[i].Cells[0].Value.ToString() == str)
                    return 0;
                i++;
            }
            return 1;
        }

        private void btnNhap_Click(object sender, EventArgs e)
        {
            if (txtTenKhuVuc.Text.ToString() != "")
                if (HopLe(dgvChiTietKhuVuc, txtTenKhuVuc.Text.ToString(),j) == 1)
                {
                    {
                        DataGridViewRow row = new DataGridViewRow();
                        dgvChiTietKhuVuc.Rows.Add(row);
                        //int i = dgvChiTietKhuVuc.RowCount - 1;
                        dgvChiTietKhuVuc.Rows[j].Cells[0].Value = txtTenKhuVuc.Text.ToString();
                        dgvChiTietKhuVuc.Rows[j].Cells[1].Value = txtSoGioNang.Value.ToString();
                        j++;
                        txtTenKhuVuc.Text = "";
                        txtSoGioNang.Value = 1;
                        txtTenKhuVuc.Focus();
                        
                    }
                }
                else
                {
                    MessageBox.Show("Khu vực đã có ở bên dưới");
                    txtTenKhuVuc.Text = "";
                    txtSoGioNang.Value = 1;
                    txtTenKhuVuc.Focus();
                }
            else
            {
                MessageBox.Show("Bạn đã nhập thiếu thông tin của khu vực");
                txtTenKhuVuc.Focus();
            }
        }

        int iRow = -1;
        private void dgvChiTietKhuVuc_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            iRow = e.Row.Index - 1;
        }

        private void btnTraCuu_Click(object sender, EventArgs e)
        {
            if (dgvChiTietKhuVuc.RowCount > 1)
            {
                KhuVucDTO kv = new KhuVucDTO();
                int i = 0;
                while (dgvChiTietKhuVuc.RowCount > 1)
                {
                    if (dgvChiTietKhuVuc.Rows[i].Cells[0].Value == null && dgvChiTietKhuVuc.Rows[i].Cells[1].Value == null)
                    {
                        dgvChiTietKhuVuc.Rows.RemoveAt(i);
                    }
                    else if (dgvChiTietKhuVuc.Rows[i].Cells[0].Value == null || dgvChiTietKhuVuc.Rows[i].Cells[1].Value == null)
                    {
                        dgvChiTietKhuVuc.CurrentCell.Selected = false;
                        dgvChiTietKhuVuc.Rows[i].Cells[0].Selected = true;
                        MessageBox.Show("Bạn nhập thiếu thông tin");
                        break;
                    }
                    else
                    {
                        kv.TenKhuVuc = dgvChiTietKhuVuc.Rows[i].Cells[0].Value.ToString();
                        kv.SoGioNang = double.Parse(dgvChiTietKhuVuc.Rows[i].Cells[1].Value.ToString());
                        KhuVucBus.ThemKhuVuc(kv);
                        dgvChiTietKhuVuc.Rows.RemoveAt(i);

                        //dgvChiTietThietBiThongDung.Rows.Remove(dgvChiTietThietBiThongDung.Rows[i]);
                    }
                }
                if (dgvChiTietKhuVuc.RowCount == 1)
                {
                    MessageBox.Show("Thêm thành công");
                    DataTable dt = KhuVucBus.DanhSachKhuVuc();
                    dgvKhuVuc.DataSource = dt;
                    j = 0;
                }
            }
            
        }

        private void txtTenKhuVuc_Leave(object sender, EventArgs e)
        {
            if (txtTenKhuVuc.Text.ToString() != "")
            {
                if (KhuVucBus.KiemTraKhuVuc(txtTenKhuVuc.Text.ToString()) == 0)
                {
                    MessageBox.Show(txtTenKhuVuc.Text.ToString() + " đã có");
                    txtTenKhuVuc.Text = "";
                    txtTenKhuVuc.Focus();
                }
            }
        }

    }
}
